package com.yyl.fmall.utils;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @BelongsProject: fmall
 * @BelongsPackage: com.yyl.fmall.utils
 * @Author: YuanSir
 * @CreateTime: 2022-11-11  09:57
 * @Description:
 * @Version: 1.0
 */

public class HttpTest {
    
    public static void main(String[] args) throws IOException {
        
        //1. 如何通过java代码访问百度？
        //hutool方法
//        String result = HttpUtil.get("https://www.baidu.com/");
//
//        System.out.println("result = " + result);
        
        //jdk方法
        String urlStr = "https://www.baidu.com/";
    
        URL url = new URL(urlStr);
        
        //建立到url的连接
        URLConnection urlConnection = url.openConnection(); //注意：这里并没有真正发送http请求，只是创建了URLConnection对象
        
        urlConnection.connect(); //真正发送http请求
        InputStream inputStream = urlConnection.getInputStream();
    
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    
        String data;
        
        while ((data = br.readLine()) != null){
    
            System.out.println(data);
    
        }
    
        
        
        br.close();
        inputStream.close();
    
    }
    
}
